﻿using System;
using MayhemCore;
using MayhemCore.ModuleTypes;
using System.Windows.Navigation;

namespace WP7Modules.Events
{
    [MayhemModule("Timer", "Triggers after a certain amount of time")]
    public class Timer : EventBase, IWp7Configurable
    {
        private System.Windows.Threading.DispatcherTimer myTimer;

        #region Configuration Properties
        private int _hours;
        public int Hours
        {
            get
            {
                return _hours;
            }
            set
            {
                _hours = value;
                SetConfigString();
            }
        }

        private int _minutes;
        public int Minutes
        {
            get
            {
                return _minutes;
            }
            set
            {
                _minutes = value;
                SetConfigString();
            }
        }

        private int _seconds;
        public int Seconds
        {
            get
            {
                return _seconds;
            }
            set
            {
                _seconds = value;
                SetConfigString();
            }
        }
        #endregion

        protected override void Initialize()
        {
            base.Initialize();

            myTimer = new System.Windows.Threading.DispatcherTimer();
            myTimer.Tick += new EventHandler(myTimer_Tick);

            // Set our defaults
            Seconds = 5;
        }

        private void myTimer_Tick(object sender, EventArgs e)
        {
            base.OnEventActivated();
        }

        private void SetInterval()
        {
            myTimer.Interval = new TimeSpan(Hours, Minutes, Seconds);
        }

        public override void SetConfigString()
        {
            ConfigString = String.Format("{0} hours, {1} minutes, {2} seconds", Hours, Minutes, Seconds);
        }
        
        #region Configuration Views
       
        public Uri Wp7Config()
        {
            return new Uri("/Modules/Pages/TimerConfig.xaml", UriKind.RelativeOrAbsolute);
        }

        #endregion

        public override void Enable()
        {
            base.Enable();
            SetInterval();
            myTimer.Start();
        }

        public override void Disable()
        {
            base.Disable();
            myTimer.Stop();
        }
    }
}
